package com.liferay.OTP;
import java.util.Random;


public class OTPkey {
	public final String OTP_HEADER_OPENTAG = "<OTPHEADER>";
	public final String OTP_HEADER_CLOSETAG = "</OTPHEADER>";
	public final String OTP_KEY_OPENTAG = "<OTP_KEY_ID>";
	public final String OTP_KEY_CLOSETAG = "</OTP_KEY_ID>";
	public final String OTP_CIPHER_OPENTAG = "<CIPHER_ID>";
	public final String OTP_CIPHER_CLOSETAG = "</CIPHER_ID>";
	public final String OTP_BITSTREAM_OPENTAG = "<BITSTREAM>";
	public final String OTP_BITSTREAM_CLOSETAG = "</BITSTREAM>";
	private String keyBitStream="";
	public String id= "";
	

	
	public OTPkey(OTPPlainText u){
		int plainTextBitLength = u.getBitStream().length();
		keyBitStream = generateRandomBits(plainTextBitLength);
		
	}

	public OTPkey(String bitStream){
		keyBitStream = bitStream;
	}

	
	/*Returns a random String of 1's and 0's (bitstream) of length bitLength*/
	private String generateRandomBits(int bitLength){
		Random randomNo = new Random();
		int roll;
		String randomStr = "";
		for (int i=0; i<bitLength; i++){
			roll = randomNo.nextInt(2);
			randomStr += String.valueOf(roll); 
		}
		return randomStr;
				
	}
	
	/*Returns the bitStream of a Key*/
	public  String getBitStream(){
		return keyBitStream;
	}
	
	/*Returns the ID of the Key*/
	public  String getID(){
		return id;
	}
	
	/*Sets the ID of the key to a newID*/
	public void  setID(String newID){
		id = newID;
	}

	public String toBody(){
		String retStr = OTP_HEADER_OPENTAG + OTP_KEY_OPENTAG +id +OTP_KEY_CLOSETAG + OTP_HEADER_CLOSETAG + OTP_BITSTREAM_OPENTAG + keyBitStream +  OTP_BITSTREAM_CLOSETAG; 
				
		return retStr;
		
	}
}
